﻿using SharpErp.Data.Common.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SharpErp.Data.Service
{
    public partial class SharpErpServiceImpl : ISharpErpService
    {
        public List<GeneralInfoItem> GetSysGeneralInfoItem(string key)
        {
            var list = GetAllKvItemClass<GeneralInfoItem>(key);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0).ToList();
        }

        public void AddGeneralInfoItem(string key, GeneralInfoItem item)
        {
            var items = GetAllKvItemClass<SharpErp.Data.Common.Entity.GeneralInfoItem>(key);
            var maxId = 1;
            if (items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
            item.Id = maxId;
            item.CreateTime = item.LastModifyTime = new DateTime();
            if (items == null) return;
            items.Add(item);
            SetAllKvItemClass(items, key);
        }

        public void UpdateGeneralInfoItem(string key, GeneralInfoItem item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<SharpErp.Data.Common.Entity.GeneralInfoItem>(key);
                for (var i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id != item.Id) continue;
                    items[i].LastModifyTime = new DateTime();
                    items[i].Name = item.Name;
                    items[i].Note = item.Note;
                    break;
                }
                SetAllKvItemClass<SharpErp.Data.Common.Entity.GeneralInfoItem>(items, key);
            }
        }

        public void DeleteGeneralInfoItem(string key, int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<SharpErp.Data.Common.Entity.GeneralInfoItem>(key);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass<SharpErp.Data.Common.Entity.GeneralInfoItem>(items, key);
            }
        }

        public List<FormulationItem> GetSysFormulationItem(string key)
        {
            var list = GetAllKvItemClass<FormulationItem>(key);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0).ToList(); ;
        }

        public void AddFormulationItem(string key, FormulationItem item)
        {
            var items = GetAllKvItemClass<SharpErp.Data.Common.Entity.FormulationItem>(key);
            var maxId = 1;
            if (items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
            item.Id = maxId;
            item.CreateTime = item.LastModifyTime = new DateTime();
            if (items == null) return;
            items.Add(item);
            SetAllKvItemClass(items, key);
        }

        public void UpdateFormulationItem(string key, FormulationItem item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<SharpErp.Data.Common.Entity.FormulationItem>(key);
                for (var i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id != item.Id) continue;
                    items[i].LastModifyTime = new DateTime();
                    items[i].Name = item.Name;
                    items[i].Formulation = item.Formulation;
                    break;
                }
                SetAllKvItemClass<SharpErp.Data.Common.Entity.FormulationItem>(items, key);
            };
        }

        public void DeleteFormulationItem(string key, int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<SharpErp.Data.Common.Entity.FormulationItem>(key);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass<SharpErp.Data.Common.Entity.FormulationItem>(items, key);
            }
        }

        public string GetSysInvoiceNextCode(int id)
        {
            if(id <= 0)
            {
                var list = GetAllKvItemClass<InvoiceSecondClass>(KVEntity.KEY_INVOICE_SECOND_CLASS);
                if (list == null || list.Count == 0) id = 1;
                else id = list.Max(p => p.Id) + 1;
            }

            return string.Format("{0:D5}", id);
        }

        public List<InvoiceFirstClass> GetSysInvoiceFirstClass()
        {
            var list = GetAllKvItemClass<InvoiceFirstClass>(KVEntity.KEY_INVOICE_FIRST_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0).ToList(); ;
        }

        public void AddInvoiceFirstClass(InvoiceFirstClass item)
        {
            var items = GetAllKvItemClass<InvoiceFirstClass>(KVEntity.KEY_INVOICE_FIRST_CLASS);
            var maxId = 1;
            if (items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
            item.Id = maxId;
            item.CreateTime = item.LastModifyTime = new DateTime();
            if (items == null) return;
            items.Add(item);
            SetAllKvItemClass<InvoiceFirstClass>(items, KVEntity.KEY_INVOICE_FIRST_CLASS);
        }

        public void UpdateInvoiceFirstClass(InvoiceFirstClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<InvoiceFirstClass>(KVEntity.KEY_INVOICE_FIRST_CLASS);
                for (var i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id != item.Id) continue;
                    items.Remove(items[i]);
                    items.Insert(i, item);
                    break;
                }
                SetAllKvItemClass<InvoiceFirstClass>(items, KVEntity.KEY_INVOICE_FIRST_CLASS);
            };
        }

        public void DeleteInvoiceFirstClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<InvoiceFirstClass>(KVEntity.KEY_INVOICE_FIRST_CLASS);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass<InvoiceFirstClass>(items, KVEntity.KEY_INVOICE_FIRST_CLASS);
            }
        }

        public List<InvoiceSecondClass> GetSysInvoiceSecondClassByParentId(int parentId)
        {
            var list = GetAllKvItemClass<InvoiceSecondClass>(KVEntity.KEY_INVOICE_SECOND_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && p.ParentId == parentId).ToList(); ;
        }

        public void AddInvoiceSecondClass(InvoiceSecondClass item)
        {
            var items = GetAllKvItemClass<InvoiceSecondClass>(KVEntity.KEY_INVOICE_SECOND_CLASS);
            var maxId = 1;
            if (items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
            item.Id = maxId;
            item.CreateTime = item.LastModifyTime = new DateTime();
            if (items == null) return;
            items.Add(item);
            SetAllKvItemClass<InvoiceSecondClass>(items, KVEntity.KEY_INVOICE_SECOND_CLASS);
        }

        public void UpdateInvoiceSecondClass(InvoiceSecondClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<InvoiceSecondClass>(KVEntity.KEY_INVOICE_SECOND_CLASS);
                for (var i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id != item.Id) continue;
                    items.Remove(items[i]);
                    items.Insert(i, item);
                    break;
                }
                SetAllKvItemClass<InvoiceSecondClass>(items, KVEntity.KEY_INVOICE_SECOND_CLASS);
            };
        }

        public void DeleteInvoiceSecondClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<InvoiceSecondClass>(KVEntity.KEY_INVOICE_SECOND_CLASS);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass<InvoiceSecondClass>(items, KVEntity.KEY_INVOICE_SECOND_CLASS);
            }
        }

        public string GetSysFeeTypeNextCode(int id)
        {
            if(id <= 0)
            {
                var list = GetAllKvItemClass<InvoiceSecondClass>(KVEntity.KEY_FEETYPE_SECOND_CLASS);
                if (list == null || list.Count == 0) id = 1;
                else id = list.Max(p => p.Id) + 1;
            }

            return string.Format("{0:D5}", id);
        }

        public List<FeeTypeFirstClass> GetSysFeeTypeFirstClass()
        {
            var list = GetAllKvItemClass<FeeTypeFirstClass>(KVEntity.KEY_FEETYPE_FIRST_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0).ToList(); ;
        }

        public void AddFeeTypeFirstClass(FeeTypeFirstClass item)
        {
            var items = GetAllKvItemClass<FeeTypeFirstClass>(KVEntity.KEY_FEETYPE_FIRST_CLASS);
            var maxId = 1;
            if (items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
            item.Id = maxId;
            item.CreateTime = item.LastModifyTime = new DateTime();
            if (items == null) return;
            items.Add(item);
            SetAllKvItemClass<FeeTypeFirstClass>(items, KVEntity.KEY_FEETYPE_FIRST_CLASS);
        }

        public void UpdateFeeTypeFirstClass(FeeTypeFirstClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<FeeTypeFirstClass>(KVEntity.KEY_FEETYPE_FIRST_CLASS);
                for (var i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id != item.Id) continue;
                    items.Remove(items[i]);
                    items.Insert(i, item);
                    break;
                }
                SetAllKvItemClass<FeeTypeFirstClass>(items, KVEntity.KEY_FEETYPE_FIRST_CLASS);
            };
        }

        public void DeleteFeeTypeFirstClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<FeeTypeFirstClass>(KVEntity.KEY_FEETYPE_FIRST_CLASS);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass<FeeTypeFirstClass>(items, KVEntity.KEY_FEETYPE_FIRST_CLASS);
            }
        }

        public List<FeeTypeSecondClass> GetSysFeeTypeSecondClassByParentId(int parentId)
        {
            var list = GetAllKvItemClass<FeeTypeSecondClass>(KVEntity.KEY_FEETYPE_SECOND_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && p.ParentId == parentId).ToList(); ;
        }

        public void AddFeeTypeSecondClass(FeeTypeSecondClass item)
        {
            var items = GetAllKvItemClass<FeeTypeSecondClass>(KVEntity.KEY_FEETYPE_SECOND_CLASS);
            var maxId = 1;
            if (items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
            item.Id = maxId;
            item.CreateTime = item.LastModifyTime = new DateTime();
            if (items == null) return;
            items.Add(item);
            SetAllKvItemClass<FeeTypeSecondClass>(items, KVEntity.KEY_FEETYPE_SECOND_CLASS);
        }

        public void UpdateFeeTypeSecondClass(FeeTypeSecondClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<FeeTypeSecondClass>(KVEntity.KEY_FEETYPE_SECOND_CLASS);
                for (var i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id != item.Id) continue;
                    items.Remove(items[i]);
                    items.Insert(i, item);
                    break;
                }
                SetAllKvItemClass<FeeTypeSecondClass>(items, KVEntity.KEY_FEETYPE_SECOND_CLASS);
            };
        }

        public void DeleteFeeTypeSecondClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<FeeTypeSecondClass>(KVEntity.KEY_FEETYPE_SECOND_CLASS);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass<FeeTypeSecondClass>(items, KVEntity.KEY_FEETYPE_SECOND_CLASS);
            }
        }

        public string GetSysOtherIncomeNextCode(int id)
        {
            if(id <= 0)
            {
                var list = GetAllKvItemClass<InvoiceSecondClass>(KVEntity.KEY_OTHERINCOME_SECOND_CLASS);
                if (list == null || list.Count == 0) id = 1;
                else id = list.Max(p => p.Id) + 1;
            }

            return string.Format("{0:D5}", id);
        }

        public List<OtherIncomeFirstClass> GetSysOtherIncomeFirstClass()
        {
            var list = GetAllKvItemClass<OtherIncomeFirstClass>(KVEntity.KEY_OTHERINCOME_FIRST_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0).ToList(); ;
        }

        public void AddOtherIncomeFirstClass(OtherIncomeFirstClass item)
        {
            var items = GetAllKvItemClass<OtherIncomeFirstClass>(KVEntity.KEY_OTHERINCOME_FIRST_CLASS);
            var maxId = 1;
            if (items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
            item.Id = maxId;
            item.CreateTime = item.LastModifyTime = new DateTime();
            if (items == null) return;
            items.Add(item);
            SetAllKvItemClass<OtherIncomeFirstClass>(items, KVEntity.KEY_OTHERINCOME_FIRST_CLASS);
        }

        public void UpdateOtherIncomeFirstClass(OtherIncomeFirstClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<OtherIncomeFirstClass>(KVEntity.KEY_OTHERINCOME_FIRST_CLASS);
                for (var i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id != item.Id) continue;
                    items.Remove(items[i]);
                    items.Insert(i, item);
                    break;
                }
                SetAllKvItemClass<OtherIncomeFirstClass>(items, KVEntity.KEY_OTHERINCOME_FIRST_CLASS);
            };
        }

        public void DeleteOtherIncomeFirstClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<OtherIncomeFirstClass>(KVEntity.KEY_OTHERINCOME_FIRST_CLASS);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass<OtherIncomeFirstClass>(items, KVEntity.KEY_OTHERINCOME_FIRST_CLASS);
            }
        }

        public List<OtherIncomeSecondClass> GetSysOtherIncomeSecondClassByParentId(int parentId)
        {
            var list = GetAllKvItemClass<OtherIncomeSecondClass>(KVEntity.KEY_OTHERINCOME_SECOND_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && p.ParentId == parentId).ToList(); ;
        }

        public void AddOtherIncomeSecondClass(OtherIncomeSecondClass item)
        {
            var items = GetAllKvItemClass<OtherIncomeSecondClass>(KVEntity.KEY_OTHERINCOME_SECOND_CLASS);
            var maxId = 1;
            if (items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
            item.Id = maxId;
            item.CreateTime = item.LastModifyTime = new DateTime();
            if (items == null) return;
            items.Add(item);
            SetAllKvItemClass<OtherIncomeSecondClass>(items, KVEntity.KEY_OTHERINCOME_SECOND_CLASS);
        }

        public void UpdateOtherIncomeSecondClass(OtherIncomeSecondClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<OtherIncomeSecondClass>(KVEntity.KEY_OTHERINCOME_SECOND_CLASS);
                for (var i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id != item.Id) continue;
                    items.Remove(items[i]);
                    items.Insert(i, item);
                    break;
                }
                SetAllKvItemClass<OtherIncomeSecondClass>(items, KVEntity.KEY_OTHERINCOME_SECOND_CLASS);
            };
        }

        public void DeleteOtherIncomeSecondClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<OtherIncomeSecondClass>(KVEntity.KEY_OTHERINCOME_SECOND_CLASS);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass<OtherIncomeSecondClass>(items, KVEntity.KEY_OTHERINCOME_SECOND_CLASS);
            }
        }

        public string GetSysCashBankNextCode(int id)
        {
            if (id <= 0)
            {
                var list = GetAllKvItemClass<InvoiceSecondClass>(KVEntity.KEY_CASHBANK_SECOND_CLASS);
                if (list == null || list.Count == 0) id = 1;
                else id = list.Max(p => p.Id) + 1;
            }

            return string.Format("{0:D5}", id);
        }

        public List<CashBankFirstClass> GetSysCashBankFirstClass()
        {
            var list = GetAllKvItemClass<CashBankFirstClass>(KVEntity.KEY_CASHBANK_FIRST_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0).ToList(); ;
        }

        public void AddCashBankFirstClass(CashBankFirstClass item)
        {
            var items = GetAllKvItemClass<CashBankFirstClass>(KVEntity.KEY_CASHBANK_FIRST_CLASS);
            var maxId = 1;
            if (items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
            item.Id = maxId;
            item.CreateTime = item.LastModifyTime = new DateTime();
            if (items == null) return;
            items.Add(item);
            SetAllKvItemClass<CashBankFirstClass>(items, KVEntity.KEY_CASHBANK_FIRST_CLASS);
        }

        public void UpdateCashBankFirstClass(CashBankFirstClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<CashBankFirstClass>(KVEntity.KEY_CASHBANK_FIRST_CLASS);
                for (var i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id != item.Id) continue;
                    items.Remove(items[i]);
                    items.Insert(i, item);
                    break;
                }
                SetAllKvItemClass<CashBankFirstClass>(items, KVEntity.KEY_CASHBANK_FIRST_CLASS);
            };
        }

        public void DeleteCashBankFirstClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<CashBankFirstClass>(KVEntity.KEY_CASHBANK_FIRST_CLASS);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass<CashBankFirstClass>(items, KVEntity.KEY_CASHBANK_FIRST_CLASS);
            }
        }

        public List<CashBankSecondClass> GetSysCashBankSecondClassByParentId(int parentId)
        {
            var list = GetAllKvItemClass<CashBankSecondClass>(KVEntity.KEY_CASHBANK_SECOND_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && p.ParentId == parentId).ToList(); ;
        }

        public List<CashBankSecondClass> GetAllSysCashBankSecondClass()
        {
            var list = GetAllKvItemClass<CashBankSecondClass>(KVEntity.KEY_CASHBANK_SECOND_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0).ToList(); ;
        }

        public void AddCashBankSecondClass(CashBankSecondClass item)
        {
            var items = GetAllKvItemClass<CashBankSecondClass>(KVEntity.KEY_CASHBANK_SECOND_CLASS);
            var maxId = 1;
            if (items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
            item.Id = maxId;
            item.CreateTime = item.LastModifyTime = new DateTime();
            if (items == null) return;
            items.Add(item);
            SetAllKvItemClass<CashBankSecondClass>(items, KVEntity.KEY_CASHBANK_SECOND_CLASS);
        }

        public void UpdateCashBankSecondClass(CashBankSecondClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<CashBankSecondClass>(KVEntity.KEY_CASHBANK_SECOND_CLASS);
                for (var i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id != item.Id) continue;
                    items.Remove(items[i]);
                    items.Insert(i, item);
                    break;
                }
                SetAllKvItemClass<CashBankSecondClass>(items, KVEntity.KEY_CASHBANK_SECOND_CLASS);
            };
        }

        public void DeleteCashBankSecondClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<CashBankSecondClass>(KVEntity.KEY_CASHBANK_SECOND_CLASS);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass<CashBankSecondClass>(items, KVEntity.KEY_CASHBANK_SECOND_CLASS);
            }
        }

        public bool AddSetAccount(SetAccountInfo item)
        {
            if (item == null || string.IsNullOrEmpty(item.Name) || string.IsNullOrEmpty(item.Admin) || string.IsNullOrEmpty(item.PasswordMd5))
                return false;
            var items = GetAllKvItemClass<SetAccountInfo>(KVEntity.KEY_SET_ACCOUNT);
            var maxId = 1;
            if (items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
            item.Id = maxId;
            item.CreateTime = item.LastModifyTime = new DateTime();
            if (items == null) return false;
            items.Add(item);
            SetAllKvItemClass<SetAccountInfo>(items, KVEntity.KEY_SET_ACCOUNT);
            return true;
        }

        public bool DeleteSetAccount(SetAccountInfo item)
        {
            var compareItem = GetSetAccountInfoById(item.Id);
            if (compareItem == null) return true;
            if(string.Equals(item.Admin, compareItem.Admin) && string.Equals(item.PasswordMd5, compareItem.PasswordMd5))
            {

                var items = GetAllKvItemClass<SetAccountInfo>(KVEntity.KEY_SET_ACCOUNT);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == item.Id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass<SetAccountInfo>(items, KVEntity.KEY_SET_ACCOUNT);
                return true;
            }
            return false;
        }

        public List<SetAccountInfo> GetSysSetAccount()
        {

            var list = GetAllKvItemClass<SetAccountInfo>(KVEntity.KEY_SET_ACCOUNT);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0).ToList(); ;
        }

        public SetAccountInfo GetSetAccountInfoById(int id)
        {
            var items = GetAllKvItemClass<SetAccountInfo>(KVEntity.KEY_SET_ACCOUNT);
            if (items == null || items.Count <= 0) return null;
            return items.Find(p => p.Id == id && (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0);
        }

        public SetAccountInfo GetSetAccountInfoByName(string name)
        {
            var items = GetAllKvItemClass<SetAccountInfo>(KVEntity.KEY_SET_ACCOUNT);
            if (items == null || items.Count <= 0) return null;
            return items.Find(p => p.Name.Equals(name) && (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0);
        }

    }
}
